package mutsumimod.cards.derivative;

import basemod.abstracts.CustomSavable;
import com.evacipated.cardcrawl.mod.stslib.fields.cards.AbstractCard.FleetingField;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.animations.VFXAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import mutsumimod.cards.BaseCard;
import mutsumimod.util.CardStats;
import mutsumimod.util.GeneralUtils;
import mutsumimod.vfx.FleetingEffect;
import mutsumimod.vfx.HealEffectWithoutAmount;

import java.util.ArrayList;
import java.util.Arrays;

public class DontRuinIt extends BaseCard implements CustomSavable<ArrayList<Integer>> {
    public static final String ID = makeID(DontRuinIt.class.getSimpleName());
    public static final CardStats info = new CardStats(
            CardColor.COLORLESS,
            CardType.SKILL,
            CardRarity.SPECIAL,
            CardTarget.SELF,
            0
    );

    public int currentHealth=-1;
    public int maxHealth=-1;

    public DontRuinIt() {
        super(ID, info);
        FleetingField.fleeting.set(this, true);
    }

    public DontRuinIt(int currentHealth,int maxHealth) {
        this();
        this.currentHealth=currentHealth;
        this.maxHealth=maxHealth;
        updateDescription();
        applyPowers();
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        addToBot(new AbstractGameAction() {
            @Override
            public void update() {
                forceSetPlayerHp();
                isDone = true;
            }
        });
        AbstractDungeon.player.masterDeck.group.stream()
                .filter(c -> c.uuid.equals(uuid))
                .forEach(c -> addToBot(new VFXAction(new FleetingEffect(c))));
    }

    @Override
    public void updateDescription() {
        rawDescription=cardStrings.DESCRIPTION;
        if(GeneralUtils.isInCombat()){
            setCustomVar("CH",currentHealth);
            setCustomVar("AH",maxHealth);
            rawDescription=cardStrings.DESCRIPTION+cardStrings.EXTENDED_DESCRIPTION[0];
        }
        initializeDescription();
    }

    @Override
    public AbstractCard makeCopy() {
        return new DontRuinIt();
    }

    private void forceSetPlayerHp() {
        AbstractPlayer p = AbstractDungeon.player;
        p.maxHealth=maxHealth;
        p.currentHealth=currentHealth;
        AbstractDungeon.topPanel.panelHealEffect();
        AbstractDungeon.effectsQueue.add(new HealEffectWithoutAmount(AbstractDungeon.player.hb.cX -
                AbstractDungeon.player.animX, AbstractDungeon.player.hb.cY));

        if ((float)p.currentHealth > (float)p.maxHealth / 2.0F && p.isBloodied) {
            p.isBloodied = false;
            for (AbstractRelic r : p.relics) {
                r.onNotBloodied();
            }
        }
        p.healthBarUpdatedEvent();
    }

    @Override
    public boolean canUpgrade(){
        return false;
    }

    @Override
    public void upgrade() {

    }

    @Override
    public DontRuinIt makeStatEquivalentCopy(){
        DontRuinIt copy = (DontRuinIt) super.makeStatEquivalentCopy();
        copy.currentHealth=currentHealth;
        copy.maxHealth=maxHealth;
        copy.updateDescription();
        return copy;
    }

    @Override
    public ArrayList<Integer> onSave() {
        return new ArrayList<>(Arrays.asList(currentHealth,maxHealth));
    }

    @Override
    public void onLoad(ArrayList<Integer> integers) {
        if(integers==null||integers.size() != 2){
            return;
        }
        currentHealth=integers.get(0);
        maxHealth=integers.get(1);
    }
}
